package com.midea.ai.air.common;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.midea.ai.air.R;

/**
 * Created by ouyangdi on 2016/3/30.
 * 带返回键的ActionBar
 */
public class ActionBarBackFragment extends BaseFragment implements View.OnClickListener{
    private RadioButton mBackButton;
    private TextView mTvTitle;
    private FrameLayout mBarFullLayout;
    private View mBotLine;
    private TextView mTvBarRight;
    private TextView mTvBarLeft;
    private ImageView mIvBarRight;

    private String mStrBack;
    private String mStrTitle;

    public void setStrBack(String strBack) {
        mStrBack = strBack;
    }

    public void setStrTitle(String strTitle) {
        mStrTitle = strTitle;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mBarFullLayout = (FrameLayout)inflater.inflate(R.layout.fragment_action_bar_back, container, false);
        initView(mBarFullLayout);
        return mBarFullLayout;
    }

    private void initView(View view) {

        mBackButton = (RadioButton)view.findViewById(R.id.back_button);
        mBackButton.setText(mStrBack);
        mBackButton.setOnClickListener(this);

        mTvTitle = (TextView)view.findViewById(R.id.tv_device);
        mTvTitle.setText(mStrTitle);

        mBotLine = view.findViewById(R.id.bot_line);

        mTvBarRight = (TextView)view.findViewById(R.id.tv_action_bar_right);
        mTvBarLeft = (TextView)view.findViewById(R.id.tv_action_bar_cancel);
        mTvBarLeft.setOnClickListener(this);

        mIvBarRight = (ImageView) view.findViewById(R.id.iv_right_btn);

        int width = getResources().getDimensionPixelOffset(R.dimen.height_12dp);
        int height = getResources().getDimensionPixelOffset(R.dimen.height_22dp);
        setDrawableLeft(mBackButton, R.drawable.register_titler_left, width, height);
    }

    public ImageView getIvBarRight() {
        return mIvBarRight;
    }

    public RadioButton getBackButton() {
        return mBackButton;
    }

    public TextView getTvTitle() {
        return mTvTitle;
    }

    public FrameLayout getBarFullLayout() {
        return mBarFullLayout;
    }

    public TextView getTvBarRight() {
        return mTvBarRight;
    }

    public TextView getTvBarLeft() {
        return mTvBarLeft;
    }

    public View getBotLine() {
        return mBotLine;
    }

    /**
     * 设置图标
     *
     * @param radioButton
     * @param drawableId
     * @param width
     * @param height
     */
    public void setDrawableLeft(RadioButton radioButton, int drawableId, int width, int height) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, width, height);
        radioButton.setCompoundDrawables(drawable, null, null, null);
    }

    public void setBackLeftIcon(int drawableId) {
        if(mBackButton != null) {
            int width = getResources().getDimensionPixelOffset(R.dimen.height_12dp);
            int height = getResources().getDimensionPixelOffset(R.dimen.height_22dp);
            setDrawableLeft(mBackButton, drawableId, width, height);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:
                getActivity().finish();
                break;

            case R.id.tv_action_bar_cancel:
                getActivity().finish();
                break;
        }
    }
}
